package com.sgb.kjwl.view.ui.activity.self.pick;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RadioGroup;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfQuoteImmediatelyBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.MachinePriceUnitEntity;
import com.sgb.kjwl.model.entity.OfferMaterialEntity;
import com.sgb.kjwl.model.entity.RateEntity;
import com.sgb.kjwl.model.entity.SuccessEntity;
import com.sgb.kjwl.model.entity.user.BusinessOpportunotiesEntity;
import com.sgb.kjwl.view.ui.activity.self.authentication.CommitSuccessActivity;
import com.sgb.kjwl.viewmodel.self.pick.SelfQuoteImmediatelyModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import static com.sgb.kjwl.view.event.SuccessType.BUSINESS_OFFER;

public class SelfQuoteImmediatelyActivity extends AppActivity {
    private ActivitySelfQuoteImmediatelyBinding binding;

    private List<RateEntity> rateEntityList;
    private List<MachinePriceUnitEntity> machinePriceUnitEntities;
    private BusinessOpportunotiesEntity businessOpportunotiesEntity;
    private OfferMaterialEntity offerMaterialEntity;
    private int ratePosition;
    private int unitPosition;
    private String rateId;
    private String no;
    @Inject
    SelfQuoteImmediatelyModel model;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Object object) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        intent.setClass(context, SelfQuoteImmediatelyActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        bundle.putSerializable("object", (Serializable) object);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(SelfQuoteImmediatelyActivity.this, R.layout.activity_self_quote_immediately);
        binding.backView.setOnClickListener(this);
        binding.rlTaxRate.setOnClickListener(this);
        binding.tvCommit.setOnClickListener(this);
        binding.rgView.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == binding.radioTwo.getId()) {
                    if (binding.radioTwo.isChecked()) {
                        binding.llRate.setVisibility(View.GONE);
                    }
                }
                if (i == binding.radioOne.getId()) {
                    if (binding.radioOne.isChecked()) {
                        binding.llRate.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    @Override
    protected void init() {
        businessOpportunotiesEntity = (BusinessOpportunotiesEntity) getIntent().getSerializableExtra("object");
        setData();
    }

    public void setData() {
        binding.tvProName.setText(businessOpportunotiesEntity.getProjName());
        binding.tvMaterialName.setText(businessOpportunotiesEntity.getName());
        binding.tvType.setText(businessOpportunotiesEntity.getSpec());
        binding.tvNumber.setText(businessOpportunotiesEntity.getNum());
    }

    @SuppressWarnings("unchecked")
    private void showRatePop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(SelfQuoteImmediatelyActivity.this, (options1, options2, options3, v) -> {
            String content = rateEntityList.get(options1).getRate();
            binding.etRate.setText(content);
            rateId = rateEntityList.get(options1).getId();
            ratePosition = options1;
        }).build();
        businessOption.setPicker(rateEntityList);
        businessOption.setSelectOptions(ratePosition);
        businessOption.show();
    }

    @SuppressWarnings("unchecked")
    private void showUnitPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(SelfQuoteImmediatelyActivity.this, (options1, options2, options3, v) -> {
            String content = machinePriceUnitEntities.get(options1).getName();
//            binding.tvUnit.setText(content);
            no = machinePriceUnitEntities.get(options1).getNo();
            unitPosition = options1;
        }).build();
        businessOption.setPicker(machinePriceUnitEntities);
        businessOption.setSelectOptions(unitPosition);
        businessOption.show();
    }

    public void getRateData(List<RateEntity> rateEntityList) {
        this.rateEntityList = rateEntityList;
        showRatePop();
    }

    public void getUnitDate(List<MachinePriceUnitEntity> machinePriceUnitEntities) {
        this.machinePriceUnitEntities = machinePriceUnitEntities;
        showUnitPop();
    }

    private void checkData() {
        if (TextUtils.isEmpty(binding.etPrice.getText())) {
            MToast.showToast(SelfQuoteImmediatelyActivity.this, "请填写送到价");
            return;
        }
        if (TextUtils.isEmpty(binding.etPrice.getText())) {
            MToast.showToast(SelfQuoteImmediatelyActivity.this, "请填写分包价");
            return;
        } else if (TextUtils.isEmpty(binding.etBrand.getText())) {
            MToast.showToast(SelfQuoteImmediatelyActivity.this, "请填写品牌");
            return;
        } else if (TextUtils.isEmpty(binding.etRate.getText()) && !binding.radioTwo.isChecked()) {
            MToast.showToast(SelfQuoteImmediatelyActivity.this, "请选择税率");
            return;
        }
        offerMaterialEntity = new OfferMaterialEntity();
        offerMaterialEntity.setNo(businessOpportunotiesEntity.getNo());
        offerMaterialEntity.setBrand(binding.etBrand.getText().toString());
        offerMaterialEntity.setPrice(binding.etPrice.getText().toString());
        if (binding.radioOne.isChecked()) {
            offerMaterialEntity.setType("1");
        } else if (binding.radioTwo.isChecked()) {
            offerMaterialEntity.setType("3");
        }
        offerMaterialEntity.setRate(rateId);
        model.commitOfferMaterial(offerMaterialEntity);
    }

    public void showSuccess() {
        SuccessEntity successEntity = new SuccessEntity();
        successEntity.setTitle("报价成功");
        successEntity.setContent("您的报价信息已提交成功");
        successEntity.setButtonText("确定");
        successEntity.setType(BUSINESS_OFFER);
        successEntity.setId(R.layout.item_business_opportunity);
        CommitSuccessActivity.start(this, successEntity);
    }

    /**
     * 隐藏软键盘
     */
    private void setSoftWareHide() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.rl_tax_rate:
                setSoftWareHide();
                if (rateEntityList != null) {
                    showRatePop();
                } else {
                    model.getRateList();
                }
                break;
            case R.id.tv_commit:
                checkData();
                break;
            /*case R.id.rl_device_price:
                setSoftWareHide();
                if (machinePriceUnitEntities != null) {
                    showUnitPop();
                } else {
//                    model.machinePriceUnit();
                }
                break;*/
        }
    }
}
